package com.recource.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.recource.annonation.Login;
import com.recource.dao.UserDao;
import com.recource.domain.User;
import com.recource.ro.UserRO;
import com.recource.util.HostHolder;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.Objects;
import java.util.UUID;

/**
 *
 */
@Controller
@RequestMapping("/user")
public class UserController {


    @RequestMapping("/detail")
    public String detail(){
        return "/admin/my/detail";
    }

    @Autowired
    HostHolder hostHolder;

    @Autowired
    private UserDao userDao;

    @RequestMapping("/my/update")
    @Login
    public String detailupdate(UserRO ro){
        User user = hostHolder.getUser();
        if(!Objects.equals(user.getId(), ro.getId())){
            throw new RuntimeException("用户不对应");
        }
        String salt = UUID.randomUUID().toString().substring(0, 5);
        user.setSalt(salt);
        StringBuilder sb = new StringBuilder();
        try {
            String password = DigestUtils.md5DigestAsHex((sb.append(salt).append(ro.getPassword())).toString().getBytes());
            user.setPassword(password);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        user.setUserType(ro.getUserType());
        user.setPhone(ro.getPhone());
        user.setUsername(ro.getUsername());
        userDao.updateById(user);
        return "redirect:/admin/index";
    }


}